﻿using System;
using System.Data.Entity;
using ICDA.DataModel.Abstract;
using ICDA.DataModel.Entity;
using System.Linq;
using System.Diagnostics;

namespace ICDA.DataModel.Concrete
{
    public class DBrepository : IRepository
    {
        private ICDAEntity context;

        public DBrepository()
        {
            if (context == null)
            {
                context = new ICDAEntity();
            }
        }
        public IQueryable<UserTables> users
        {
            get { return context.UsersDB; }
        }
        public IQueryable<LeaveTables> leave
        {
            get { return context.LeaveDB; }
        }
        public IQueryable<ActivityTables> activity
        {
            get { return context.ActivityDB; }
        }
        public bool AddUser(UserTables user)
        {
            try
            {
                context.UsersDB.Add(user);
                return true;
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }
        /// <summary>
        /// Adds the task.
        /// </summary>
        /// <param name="act">The act.</param>
        /// <returns>True or False</returns>
        public bool AddTask(ActivityTables act)
        {
            try
            {
                context.ActivityDB.Add(act);
                return true;
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// Updates the user.
        /// </summary>
        /// <param name="usr">The usr.</param>
        /// <returns>True or False</returns>
        public bool UpdateUser(UserTables usr)
        {
            try
            {
                context.UsersDB.Add(usr);
                return true;
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }
        public bool DeleteUser(UserTables usr)
        {
            try
            {
                context.UsersDB.Remove(usr);
                return true;
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }
        /// <summary>
        /// Adds the leave.
        /// </summary>
        /// <param name="leave">The leave.</param>
        /// <returns>True or False</returns>
        public bool AddLeave(LeaveTables leave)
        {
            try
            {
                context.LeaveDB.Add(leave);
                return true;
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// Deletes the leave.
        /// </summary>
        /// <param name="leave">The leave.</param>
        /// <returns>True or False</returns>
        public bool DeleteLeave(LeaveTables leave)
        {
            try
            {
                context.LeaveDB.Remove(leave);
                return true;
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// Changes the password.
        /// </summary>
        /// <param name="usr">The usr.</param>
        /// <returns>True/False</returns>
        public bool ChangePassword(UserTables usr)
        {
            try
            {
                context.UsersDB.Add(usr);
                return true;
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// Deletes the task.
        /// </summary>
        /// <param name="act">The act.</param>
        /// <returns>True or false</returns>
        public bool DeleteTask(ActivityTables act)
        {
            try
            {
                context.ActivityDB.Remove(act);
                return true;
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// Saves the changes.
        /// </summary>
        /// <returns>True or False</returns>
        public bool SaveChanges()
        {
            bool success = false;
            try
            {
                context.SaveChanges();
                success = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return success;
        }

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources
        /// </summary>
        public void Dispose()
        {
            if (context != null)
            {
                context.Dispose();
                context = null;
            }
        }
    }
}
